package com.ququ.syb.entrance.service;

import com.alibaba.fastjson.JSON;
import com.ququ.common.result.Result;
import com.ququ.common.result.ResultJson;
import com.ququ.common.utils.BeanUtil;
import com.ququ.common.utils.MapUtil;
import com.ququ.common.utils.RegExpUtil;
import com.ququ.syb.common.constant.EncryptTypeConstant;
import com.ququ.syb.common.pojo.AccountInfo;
import com.ququ.syb.entrance.fegin.AccountBusinessFegin;
import com.ququ.syb.entrance.po.RequestData;
import com.ququ.syb.entrance.vo.SybResult;
import com.ququ.syb.entrance.vo.SybResultData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class AccountService {

    private Logger logger = LoggerFactory.getLogger(AccountService.class);
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private AccountBusinessFegin accountBusinessFegin;
    @Autowired
    private SecurityService securityService;

    public SybResult getAccountInfo(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = securityService.getParametersMap(requestData);
        logger.debug("请求参数:{}",dataMap);
        AccountInfo accountInfo = accountBusinessFegin.getAccountInfo(requestData.getAccountid()).getData();
        if (accountInfo == null)
            return new SybResult(-1,"账户不存在",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> resultMap = JSON.parseObject(JSON.toJSONString(accountInfo),Map.class);
        logger.debug("转化后Map:{}",resultMap);
        resultMap.remove("password");
        resultMap.remove("token");
        resultMap.put("createtime",simpleDateFormat.format(accountInfo.getCreatetime()));
        resultMap.put("lastmodifytime",simpleDateFormat.format(accountInfo.getLastmodifytime()));
        return securityService.buildSybResult(resultMap,requestData);
    }


    public SybResult getToken(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.PLAINTEXT)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        String token = accountBusinessFegin.getToken(requestData.getAccountid()).getData();
        if (token == null)
            return new SybResult(-1,"账户不存在",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("token",token);
        return securityService.buildSybResult(resultMap,requestData);
    }


    //注册生意宝账户
    public SybResult register(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.PLAINTEXT)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = JSON.parseObject(requestData.getData(),Map.class);
        logger.debug("请求参数:{}",dataMap);
        if (!dataMap.containsKey("mobile") || !(RegExpUtil.isMobile(dataMap.get("mobile").toString()) || dataMap.get("mobile").toString().startsWith("100")))
            return new SybResult(-1,"缺少手机号参数或手机号不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Result<AccountInfo> result = accountBusinessFegin.register(dataMap.containsKey("accountid")?dataMap.get("accountid").toString():null,
                dataMap.containsKey("name")?dataMap.get("name").toString():requestData.getOperationname(),
                dataMap.get("mobile").toString(),
                dataMap.containsKey("accountname")?dataMap.get("accountname").toString():requestData.getOperationname(),
                requestData.getAppcode(),
                dataMap.containsKey("partner")?dataMap.get("partner").toString():null,
                dataMap.containsKey("idcard")?dataMap.get("idcard").toString():null,
                dataMap.containsKey("password")?dataMap.get("password").toString():null);
        if (result.getStatus() != 1)
            return new SybResult(result.getStatus(),result.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> resultMap = JSON.parseObject(JSON.toJSONString(result.getData()),Map.class);
        logger.debug("转化后Map:{}",resultMap);
        resultMap.remove("password");
//        resultMap.remove("token");
        resultMap.put("createtime",simpleDateFormat.format(result.getData().getCreatetime()));
        resultMap.put("lastmodifytime",simpleDateFormat.format(result.getData().getLastmodifytime()));
        resultMap.put("token",result.getData().getToken().substring(8,24));
        requestData.setAccountid(result.getData().getAccountid());
        return securityService.buildSybResult(resultMap,requestData);
    }


    public SybResult modify(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = securityService.getParametersMap(requestData);
        logger.debug("请求参数:{}",dataMap);
        if (!dataMap.containsKey("mobile") || !(RegExpUtil.isMobile(dataMap.get("mobile").toString()) || dataMap.get("mobile").toString().startsWith("100")))
            return new SybResult(-1,"缺少手机号参数或手机号不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Result<AccountInfo> result = accountBusinessFegin.update(requestData.getAccountid(),
                dataMap.containsKey("name")?dataMap.get("name").toString():requestData.getOperationname(),
                dataMap.containsKey("mobile")?dataMap.get("mobile").toString():null,
                dataMap.containsKey("accountname")?dataMap.get("accountname").toString():requestData.getOperationname(),
                null,
                dataMap.containsKey("partner")?dataMap.get("partner").toString():null,
                dataMap.containsKey("idcard")?dataMap.get("idcard").toString():null,
                null,null,null,null);
        if (result.getStatus() != 1)
            return new SybResult(result.getStatus(),result.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> resultMap = JSON.parseObject(JSON.toJSONString(result.getData()),Map.class);
        logger.debug("转化后Map:{}",resultMap);
        resultMap.remove("password");
        resultMap.remove("token");
        resultMap.put("createtime",simpleDateFormat.format(result.getData().getCreatetime()));
        resultMap.put("lastmodifytime",simpleDateFormat.format(result.getData().getLastmodifytime()));
        return securityService.buildSybResult(resultMap,requestData);
    }


    public SybResult setPassword(RequestData<String> requestData){
        if (requestData.getEncryptType() != EncryptTypeConstant.AES)
            return new SybResult(-1,"加密方式不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        Map<String,Object> dataMap = securityService.getParametersMap(requestData);
        logger.debug("请求参数:{}",dataMap);
        if (!dataMap.containsKey("password") || dataMap.get("password").toString().length() != 32)
            return new SybResult(-1,"新密码为空或者长度不正确",securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        ResultJson result = accountBusinessFegin.setPassword(requestData.getAccountid(),dataMap.get("password").toString());
        if (result.getStatus() != 1)
            return new SybResult(result.getStatus(),result.getMsg(),securityService.sign(new SybResultData(requestData.getAccountid(),""),requestData.getAppcode()),requestData.getAccountid());
        return securityService.buildSybResult(new HashMap<>(),requestData);
    }


}
